
import '../event/check_status_event.dart';
import '../event/door_status_event.dart';
import '../event/inventory_event.dart';
import '../event/read_block_event.dart';
import '../main.dart';
import '../util/convert_util.dart';
import 'inventory_manager.dart';

class AnalysisHelper{

  String result = "";
  bool isAnalyze = false;

  // Set<(String,String)> set = {};
  Set<String> set = {};

  InventoryManager inventoryManager = InventoryManager();

  dataReceived(hexString){
    result += hexString;


    //询查标签
    if(result.length > 12 && result.substring(10,12) == "01"){
      // result += hexString;
      analyze();
      return;
    }
    //读块
    if(result.length > 12 && result.substring(10,12)=="23"){
      // result += hexString;
      analyzeBlock();
      return;
    }

    //开灯 关灯1
    if(result.length >= 24 && result.substring(10,16)=="C15900"){
      result= '';
      // eventFactory.fire(LightStatusEvent());
      return;
    }
    //开灯 关灯2
    if(result.length >= 24 && result.substring(10,16)=="C15901"){
      result= '';
      // eventFactory.fire(LightStatusEvent());
      return;
    }
    //开门
    if(result.length >= 24 && result.substring(10,16)=="C25900"){
      result= '';
      eventFactory.fire(DoorStatusEvent(true));
      return;
    }
    //查询状态
    if(result.length >= 28 && result.substring(10,14)=="C359" && result.substring(22,24)=="00"){
      checkStatus(result);
      return;
    }

    //关门被动反馈
    if(result.length >= 28 &&result.substring(10,14)=="C359" && result.substring(22,24)=="01" && result.substring(18,20) == '01'){
      result= '';
      eventFactory.fire(DoorStatusEvent(false));
      return;
    }
    //开门被动反馈
    if(result.length >= 28 &&result.substring(10,14)=="C359" && result.substring(22,24)=="01" && result.substring(18,20) == '00'){
      result= '';
      return;
    }

  }

  // analyzeId(String ids){
  //   String id = ids;
  //   if(ids.endsWith('AA01EA')){
  //     id = ids.replaceAll('AA01EA', '');
  //   }
  //   if(id.length>10){
  //     String uid = id.substring(id.length-8,id.length);
  //     String exUid = uid.substring(6,8)+uid.substring(4,6)+uid.substring(2,4)+uid.substring(0,2);
  //     String uidNo = hexToInt(exUid).toString();
  //     while(uidNo.length<10){
  //       uidNo = '0$uidNo';
  //     }
  //     // eventFactory.fire(UserIdEvent(uidNo));
  //   }
  // }

  analyze(){

    print('result=$result');
    if(isAnalyze){
      return;
    }
    isAnalyze = true;
    //盘点结束指令
    if(result.startsWith("DD11EF0C") && result.length>=24){
      var data = set.toList();
      // eventFactory.fire(InventoryEvent(data));
      inventoryManager.setInventoryResult(data);
      result = "";
      set.clear();
      isAnalyze = false;
      return;
    }
    if(result.length>=38){
      set.add((result.substring(18,34)));
      // set.add((result.substring(18,34),result.substring(8,10)));
      result = result.substring(38);
      isAnalyze = false;
      if(result.isNotEmpty){
        analyze();
      }
    }else{
      isAnalyze = false;
    }
  }

  analyzeBlock() {
    print('result=$result');
    if(isAnalyze){
      return;
    }
    isAnalyze = true;
    if (result.length  >= 24 && result.substring(12, 14) == "95") {
      result = '';
      isAnalyze = false;
      inventoryManager.setReadBlockResult(null);
      // eventFactory.fire(ReadBlockEvent(null));
      return;
    }
    String blockCount = result.substring(12, 14);
    int count = hexToInt(blockCount);
    if (result.length >= 16+count*2+count*8+4) {
      var block = '';
      for(int i = 0; i<count;i++){
        int start = 16+(i+1)*2+i*8;
        block = block + result.substring(start,start+8);
      }
      result = '';
      isAnalyze = false;
      inventoryManager.setReadBlockResult(block);

      // eventFactory.fire(ReadBlockEvent(block));
      // int? len = int.tryParse(block.substring(0,2));
      // if(len != null && block.length>len+2){
      //   var no = block.substring(2,len+2);
      //   eventFactory.fire(ReadBlockEvent(no));
      // }else{
      //   eventFactory.fire(ReadBlockEvent(null));
      // }
    }else{
      result = '';
      isAnalyze = false;
      inventoryManager.setReadBlockResult(null);
      // eventFactory.fire(ReadBlockEvent(null));
    }
  }
  //
  //
  // analyze(){
  //   if(isAnalyze){
  //     return;
  //   }
  //   isAnalyze = true;
  //   if(result.startsWith("DD11EF0CFF01")  && result.length>=24){
  //     result = "";
  //     eventFactory.fire(InventoryEvent("",true));
  //     isAnalyze = false;
  //   }
  //   if(result.length>=38){
  //     eventFactory.fire(InventoryEvent(result.substring(18,34),false));
  //     result = result.substring(38);
  //     isAnalyze = false;
  //     if(result.isNotEmpty){
  //       analyze();
  //     }
  //   }else{
  //     isAnalyze = false;
  //   }
  // }

  // readBlock(hexString) {
  //   if (hexString.length > 14 && hexString.substring(12, 14) == "95") {
  //     eventFactory.fire(ReadBlockEvent("", false));
  //     return;
  //   }
  //   String blockCount = hexString.substring(12, 14);
  //   int count = hexToInt(blockCount);
  //   if (hexString.length >= 16+count*2+count*8+4) {
  //     var block = '';
  //     for(int i = 0; i<count;i++){
  //       int start = 16+(i+1)*2+i*8;
  //       block = block + hexString.substring(start,start+8);
  //     }
  //     eventFactory.fire(ReadBlockEvent(block, true));
  //   }
  // }

  checkStatus(hexString){
    bool light1 = false;
    bool light2 = false;
    bool door = false;
    if(hexString.substring(14,16)=="00"){
      light1 = false;
    }else{
      light1 = true;
    }
    if(hexString.substring(16,18)=="00"){
      light2 = false;
    }else{
      light2 = true;
    }
    if(hexString.substring(18,20)=="01"){
      door = false;
    }else{
      door = true;
    }
    eventFactory.fire(CheckStatusEvent(light1, light2,door));
    result= '';
  }

  clear(){
    result= '';
    isAnalyze = false;
  }

}